let Koa = require('koa');

let app = new Koa();

let query = require('./db/index');

let bodyparser = require('koa-bodyparser');



let Router = require('koa-router');

let router = new Router();

app.use(async (ctx,next) => {
    ctx.request.pp = 'xxx';
    await next()
})


app.use(bodyparser());

app.use(router.routes())
    .use(router.allowedMethods())



//1.添加
router.post('/api/add',async (ctx) => {
    //1.>前端传递的信息,做容错处理
    console.log(ctx.request.body);

    let {title,con,author} = ctx.request.body;

    if(title && con && author){
        //错误处理机制
        try{
            await query('insert into bloglist (title,con,author,time,num) values (?,?,?,?,?)',[title,con,author,new Date(),0])
            ctx.body = {
                code:1,
                msg:'发布成功'
            }
        }catch(e){
            ctx.body = {
                code:0,
                msg:e
            }
        }
    }else{
        ctx.body = {
            code:2,
            msg:'参数有误'
        }
    }

})

//2.删除
router.delete('/api/del',async (ctx) => {
    let {id} = ctx.request.query;
    if(id){
        try{
            await query('delete from bloglist where id=?',[id]);

            ctx.body = {
                code:1,
                msg:'删除成功'
            }
        }catch(e){
            ctx.body = {
                code:0,
                msg:e
            }
        }
    }else{
        ctx.body = {
            code:2,
            msg:'参数有误'
        }
    }
})

//查询
router.get('/api/list',async (ctx) => {
    //
    let {author} = ctx.request.query;

    let sql = 'select * from bloglist';

    sql += author ? ' where author=?' : '';

    let result = await query(sql,[author]);
    ctx.body = {
        code:1,
        data:result
    }
})

//模糊搜索
router.get('/api/search',async (ctx) => {
    let {q} = ctx.request.query;
    
    let result = await query(`select * from bloglist where title like '%${q}%'`);

    ctx.body = {
        code:1,
        data:result
    }
})

//修改
router.put('/api/edit',async (ctx) => {
    //id title con 
    let {id,title,con} = ctx.request.body;

    if(id && title && con){
        try{
            await query('update bloglist set title=?,con=? where id=?',[title,con,id]);
            ctx.body = {
                code:1,
                msg:'修改成功'
            }
        }catch(e){
            ctx.body = {
                code:0,
                msg:e
            }
        }
    }else{
        ctx.body = {
            code:2,
            msg:'参数有误'
        }
    }
})

//阅读
router.put('/api/read',async (ctx) => {
    let {id,num} = ctx.request.body;
    if(id && num){
        try{
            await query('update bloglist set num=? where id=?',[num+1,id]);
            ctx.body = {
                code:1,
                msg:'已阅读'
            }
        }catch(e){
            ctx.body = {
                code:0,
                msg:e
            }
        }
    }else{
        ctx.body = {
            code:2,
            msg:'参数有误'
        }
    }
})

//登录
router.post('/api/login',async (ctx) => {
    //1.接收前端传递的用户名 ，密码  ctx.request.body

    //2.容错处理，参数的判断
    let {username,password} = ctx.request.body;

    if(username && /\d{3,6}/.test(password)){
        let target = await query('select * from userlist where username=? and password=?',[username,password]);

        if(target.length){
            //登录
            ctx.body = {
                code:1,
                msg:'登录成功'
            }
        }else{
            //
            ctx.body = {
                code:0,
                msg:'登录失败'
            }
        }
    }else{
        ctx.body = {
            code:2,
            msg:'参数有误'
        }
    }

})

//注册
router.post('/api/registry',async (ctx) =>{
    //1.接收前端传递的用户名 ，密码  ctx.request.body

    //2.容错处理，参数的判断

    let {username,password} = ctx.request.body;

    if(username && /\d{3,6}/.test(password)){
        //3.查询此人是否存在
        let target = await query('select * from userlist where username=?',[username]);
        console.log(target)
        if(target.length){
            //4.1 存在
            ctx.body = {
                code:4,
                msg:'用户名已注册'
            }
        }else{
            //4.2 不存在  添加
            try{

                await query('insert into userlist (username,password) values (?,?)',[username,password]);

                ctx.body = {
                    code:1,
                    msg:'注册成功'
                }

            }catch(e){
                ctx.body = {
                    code:0,
                    msg:e
                }
            }
        }
    }else{
        ctx.body = {
            code:2,
            msg:'参数有误'
        }
    }

})

//详情
router.get('/api/detail',async (ctx) => {
    let {id} = ctx.request.query;
    if(id){
        let target = await query('select * from bloglist where id=?',[id]);
        ctx.body = {
            code:1,
            data:target[0]
        }
    }else{
        ctx.body = {
            code:0,
            msg:'参数有误'
        }
    }
})

app.listen(3000,() => {
    console.log("服务器启动成功")
})